package websocket

import (
	"websocket"
)

type Client struct {
	ws       *websocket.Conn
	Online   bool
	Nickname string
}

func newClient(ws *websocket.Conn) *Client {
	c := &Client{ws, false, ""}
	c.handleConnect()
	return c
}

func (c *Client) handleConnect() {
	println("Hey!")
}

func (c *Client) handleDisconnect() {
	println("Hey...")
}

func (c *Client) handle() {
	for {
		data := make([]byte, 1024)
		n, err := c.ws.Read(data)
		// The shortest acceptable message contains 5 bytes.
		// ["a"] for example, is a message of type a with no arguments.
		if err != nil || n < 5 {
			break
		}
		msg, err := parse(data[:n])
		if err != nil {
			break
		}
		wmChan <- worldMessage{c, msg}
	}
}

func (c *Client) send(msg interface{}) {
	data, _ := compose(msg)
	c.ws.Write(data)
}

func (c *Client) close() {
	c.ws.Close()
	c.handleDisconnect()
}
